package com.weifeiyang.aidlserverdemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    @Bind(R.id.btn_start)
    Button btnStart;
    @Bind(R.id.btn_stop)
    Button btnStop;
    @Bind(R.id.btn_bind_1)
    Button btnBind1;
    @Bind(R.id.btn_bind_2)
    Button btnBind2;
    @Bind(R.id.btn_unbind_1)
    Button btnUnbind1;
    @Bind(R.id.btn_unbind_2)
    Button btnUnbind2;
    @Bind(R.id.text1)
    TextView text1;
    @Bind(R.id.text2)
    TextView text2;

    private MySingleService.MyBinder mBinder;
    private MySingleNotifyCallBackInterface callBack;
    private MySingleServiceConnection conn1;
    private MySingleServiceConnection conn2;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ServiceManagerUtil.dealWithMessage(msg, text1);
                    break;
                case 2:
                    ServiceManagerUtil.dealWithMessage(msg, text2);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();

    }

    private void initView() {

        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        btnBind1.setOnClickListener(this);
        btnBind2.setOnClickListener(this);
        btnUnbind1.setOnClickListener(this);
        btnUnbind2.setOnClickListener(this);

        callBack = new MySingleNotifyCallBackInterface() {
            @Override
            public void notifyMainUiThread(int from, String methodName, String data) {
                Message msg = new Message();
                msg.obj = data;
                msg.what = from;
                Bundle bundle = new Bundle();
                bundle.putString("methodName", methodName);
                msg.setData(bundle);
                mHandler.sendMessage(msg);
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                Intent startIntent = new Intent(this, MySingleService.class);
                startService(startIntent);
                break;
            case R.id.btn_stop:
                Intent stopIntent = new Intent(this, MySingleService.class);
                stopService(stopIntent);
                break;
            case R.id.btn_bind_1:
                conn1 = new MySingleServiceConnection(1);
                Intent bindIntent1 = new Intent(this, MySingleService.class);
                bindService(bindIntent1, conn1, BIND_AUTO_CREATE);
                break;
            case R.id.btn_bind_2:
                conn2 = new MySingleServiceConnection(2);
                Intent bindIntent2 = new Intent(this, MySingleService.class);
                bindService(bindIntent2, conn2, BIND_AUTO_CREATE);
                break;

            case R.id.btn_unbind_1:
                if (conn1 != null) {
                    unbindService(conn1);
                    Toast.makeText(this, "解绑 service1 成功", Toast.LENGTH_SHORT).show();
                    conn1 = null;
                }
                break;
            case R.id.btn_unbind_2:
                if (conn2 != null) {
                    Toast.makeText(this, "解绑 service2 成功", Toast.LENGTH_SHORT).show();
                    unbindService(conn2);
                    conn2 = null;
                }
                break;
        }
    }

    /*
        绑定链接
     */
    class MySingleServiceConnection implements ServiceConnection {

        private int fromPosition;

        public MySingleServiceConnection(int from) {
            fromPosition = from;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBinder = ((MySingleService.MyBinder) service);
            mBinder.registerCallBack(fromPosition, callBack);
            mBinder.startService();
            ServiceManagerUtil.dealWithData(fromPosition, mBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

}
